<template>
  <el-dialog
    :title="title"
    :visible.sync="visibleShow"
    width="85%"
    :destroy-on-close="true"
    :append-to-body="true"
    @close="handleClose"
  >
    <!-- 表格   -->
    <el-table v-loading="loading" :data="tableData">
      <el-table-column label="序号" align="center" type="index" width="50">
        <template slot-scope="{ $index }">
          <span>{{
            (pagination.pageNum - 1) * pagination.pageSize + $index + 1
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="会话编号"
        :show-overflow-tooltip="true"
        align="center"
        prop="sessionId"
      />
      <el-table-column
        label="电话"
        align="center"
        prop="userTel"
        :formatter="
          ({ userTel }) => {
            return desensitizePhone(userTel);
          }
        "
      />
      <el-table-column label="坐席编号" align="center" prop="robotId" />
      <el-table-column
        label="内容"
        align="center"
        :show-overflow-tooltip="true"
        prop="questionText"
      />
      <el-table-column label="来源" align="center" prop="source">
      </el-table-column>
      <el-table-column
        label="通话时长（秒）"
        align="center"
        prop="callDuration"
        width="180"
      >
      </el-table-column>
      <el-table-column
        label="会话时间"
        align="center"
        prop="createTime"
        :show-overflow-tooltip="true"
      ></el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleEdit(scope.row)"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="pagination.pageNum"
      :limit.sync="pagination.pageSize"
      @pagination="getList"
    />

    <!-- 编辑 -->
    <edit-dialog
      :editShow="editShow"
      :questionText="questionText"
      :id="id"
      @onCloseEditDialog="onCloseEditDialog"
    ></edit-dialog>
  </el-dialog>
</template>

<script>
import { getMsgList } from "@/api/customerService/session";
import editDialog from "./editDialog.vue";

export default {
  name: "messageListDialog",
  components: { editDialog },
  props: {
    messageDialogShow: {
      type: Boolean,
      default: false,
    },
    sessionId: {
      type: String,
      default: "",
    },
    title: {
      type: String,
      default: "消息列表",
    },
  },
  data() {
    return {
      loading: false,
      tableData: [], //表格数据
      pagination: {
        pageNum: 1,
        pageSize: 10,
      },
      total: 0,
      editShow: false,
      id: "",
      questionText: "",
      visibleShow: false,
    };
  },

  watch: {
    messageDialogShow: {
      handler(val) {
        this.visibleShow = val;
      },
      immediate: true,
    },

    sessionId: {
      handler(newVal, oldVal) {
        if (newVal) {
          this.getList();
        }
      },
      immediate: true,
    },
  },
  methods: {
    /** 查询字典数据列表 */
    async getList() {
      let params = {
        sessionId: this.sessionId,
        ...this.pagination,
      };
      this.loading = true;
      const { total, rows } = await getMsgList(params);
      this.total = total;
      this.tableData = rows;
      this.loading = false;
    },

    // 编辑
    handleEdit({ id, questionText }) {
      this.editShow = true;
      this.id = id;
      this.questionText = questionText;
    },

    // 关闭编辑弹窗
    onCloseEditDialog() {
      this.editShow = false;
      this.id = "";
      this.questionText = "";
      this.getList();
    },

    // 手机号脱敏
    desensitizePhone(phone) {
      if (!phone) {
        return "";
      }
      const reg = /(\d{3})\d{4}(\d{4})/;
      return phone.replace(reg, (match, p1, p2) => `${p1}****${p2}`);
    },

    handleClose() {
      this.visibleShow = false;
      this.$emit("onCloseMessageDialog");
    },
  },
};
</script>

<style scoped></style>
